package com.ys.service.business.impl;

import java.util.Map;

import com.ys.entity.flow.YspModel;
import com.ys.entity.sys.YsMenu;
import com.ys.enume.flow.FlowEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ys.entity.business.TpDeptUserPoint;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.mapper.business.TpDeptUserPointMapper;
import com.ys.service.business.TpDeptUserPointService;
import com.ys.service.flow.YspTaskService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.R;
import org.springframework.transaction.annotation.Transactional;

@Service("tpDeptUserPointService")
public class TpDeptUserPointServiceImpl implements TpDeptUserPointService {

	@Autowired
	private TpDeptUserPointMapper tpDeptUserPointMapper;
	@Autowired
	private YspTaskService yspTaskService;

	
	@Override
	public R add(Map<String, Object> params,Long menuId) throws Exception {
		TpDeptUserPoint entity = JSON.parseObject(JSON.toJSONString(params), TpDeptUserPoint.class);
		entity.setStatus(1);
		entity.setEffectStock(0);
		tpDeptUserPointMapper.insert(entity);

		//新增审批流
		String bid = entity.getBid();
		YsMenu ysMenu = new YsMenu().setId(menuId).queryFirst();
		if (ysMenu.getFlowModelId().longValue() != 0L) {
			YspModel yspModel = new YspModel().setId(ysMenu.getFlowModelId()).queryFirst();
			String codeModel = yspModel.getCode();
			String createUserId = ShiroUtils.getUserId();
			yspTaskService.init(codeModel, bid, createUserId);
		}

		//return R.ok("操作成功");
		return R.ok("/web/business/tpDeptUserPoint/toDeptUserPointEditOrDetail?id=" + entity.getId() + "&type=1");
	}

	@Override
	public R edit(Map<String, Object> params) {
		TpDeptUserPoint entity = JSON.parseObject(JSON.toJSONString(params), TpDeptUserPoint.class);

		//判断审批是否已流转
		YsProcInst ysProcInst = new YsProcInst().setBid(params.get("bid").toString()).queryFirst();
		if(ysProcInst != null && ysProcInst.getStateFlow() != YsProcInstEnum.StateFlow.INIT.value){
			return R.error("已流转，无法编辑");
		}

		entity.setStatus(1);
		tpDeptUserPointMapper.update(entity);
		return R.ok("操作成功");
	}

	@Override
	public R deleteDeptUserPoint(long id) throws Exception {

		TpDeptUserPoint entity = new TpDeptUserPoint().setId(id).queryFirst();
		if (entity.getEffectStock() == FlowEnum.EffectStock.EFFECTED.value) {
			return R.error("已生效数据无法删除");
		}

		tpDeptUserPointMapper.deleteTrue(id);

		String loginUserId = ShiroUtils.getUserId();
		yspTaskService.flowDelete(entity.getBid(), loginUserId);

		return R.ok("操作成功");
	}

	@Override
	@Transactional
	public void flowFinish(Long id) throws Exception {

		TpDeptUserPoint entity = new TpDeptUserPoint().setId(id).queryFirst();

		YsProcInst ysProcInst = new YsProcInst().setBid(entity.getBid()).queryFirst();
		if (ysProcInst.getStateFlow() != YsProcInstEnum.StateFlow.FINISH.value) {
			return;
		}

		entity.setEffectStock(FlowEnum.EffectStock.EFFECTED.value);
		tpDeptUserPointMapper.update(entity);
	}
}
